﻿// COPYRIGHT 2012, Justin Michalczak
//
// This file is licenced to the public under the Apache
// License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CopperVineSoftware.Stackr.Parser;
using CopperVineSoftware.Stackr.Parser.Lexemes;

namespace CopperVineSoftware.Stackr.Compiler.DCPU16Assembly
{
    class BuiltInFunctionDefinition : Definition
    {
        public static IEnumerable<BuiltInFunctionDefinition> GetBuiltInFunctions()
        {
            var resourceSet = BuiltIns.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.InvariantCulture, true, true);
            foreach(System.Collections.DictionaryEntry entry in resourceSet)
            {
                string name = entry.Key.ToString();
                string body = entry.Value.ToString();
                string[] splitBody = body.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                yield return new BuiltInFunctionDefinition(name, splitBody);
            }
        }

        private readonly string name;
        private readonly IEnumerable<string> instructions;

        /// <summary>The name to bind the function to.</summary>
        public string Name
        {
            get
            {
                return this.name;
            }
        }

        /// <summary>The raw instruction set for the function.</summary>
        public IEnumerable<string> Instructions
        {
            get
            {
                return this.instructions;
            }
        }

        /// <summary>Constructor.</summary>
        /// <param name="name">Name of the built-in function.</param>
        /// <param name="instructions">Raw instructions making up the body of the function.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="name"/> is <c>null</c> or empty.</exception>
        /// <exception cref="ArgumentNullException">If <paramref name="instructions"/> is <c>null</c>.</exception>
        public BuiltInFunctionDefinition(string name, IEnumerable<string> instructions)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");
            if (instructions == null)
                throw new ArgumentNullException("instructions");

            this.name = name;
            this.instructions = instructions;
        }
    }
}
